﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using DataFileProvider;

namespace FileAccessTests
{
    [TestClass]
    public class BinarySearchTest
    {
        [TestMethod]
        public void CanSearchOneLine()
        {
            string path = @"..\..\TestFiles\one.txt";
            FileInfo file = new FileInfo(path);
            WordNetFile fileSource = new WordNetFile(file);
            
            Assert.IsNull(fileSource.Search("abate"));
            Assert.IsNull(fileSource.Search("abbey"));

            Assert.IsTrue(fileSource.Search("abash").StartsWith("abash "));
        }

        [TestMethod]
        public void CanSearchTwoLines()
        {
            string path = @"..\..\TestFiles\two.txt";
            FileInfo file = new FileInfo(path);
            WordNetFile fileSource = new WordNetFile(file);

            //Assert.IsNull(fileSource.Search("abbey"));

            Assert.IsTrue(fileSource.Search("abate").StartsWith("abate "));
            Assert.IsTrue(fileSource.Search("abash").StartsWith("abash "));
        }

        [TestMethod]
        public void CanSearchThreeLines()
        {
            string path = @"..\..\TestFiles\three.txt";
            FileInfo file = new FileInfo(path);
            WordNetFile fileSource = new WordNetFile(file);

            Assert.IsTrue(fileSource.Search("abbey").StartsWith("abbey "));
            Assert.IsTrue(fileSource.Search("abate").StartsWith("abate "));
            Assert.IsTrue(fileSource.Search("abash").StartsWith("abash "));
        }

        [TestMethod]
        public void CanSearchFirst()
        {
            string path = @"..\..\TestFiles\tiny.txt";
            FileInfo file = new FileInfo(path);
            WordNetFile fileSource = new WordNetFile(file);
            string expectedValue = "'abash yuiuyiu 789 iuyiyi 8966765";
            Assert.AreEqual(expectedValue, fileSource.Search("'abash").Trim());
            Assert.IsNull(fileSource.Search("aba"));
        }
        
        [TestMethod]
        public void CanSearchLast()
        {
            string path = @"..\..\TestFiles\tiny.txt";
            FileInfo file = new FileInfo(path);
            WordNetFile fileSource = new WordNetFile(file);
            string expectedValue = "vexed zzzzz ww dfhfdhh 5547454";
            Assert.AreEqual(expectedValue, fileSource.Search("vexed").Trim());
            Assert.IsNull(fileSource.Search("vexe"));
        }

        [TestMethod]
        public void CanSearchMiddle()
        {
            string path = @"..\..\TestFiles\tiny.txt";
            FileInfo file = new FileInfo(path);
            WordNetFile fileSource = new WordNetFile(file);
            string expectedValue = "humpback ccc eee";
            Assert.AreEqual(expectedValue, fileSource.Search("humpback").Trim());
            Assert.IsNull(fileSource.Search("humpbac"));
        }

        [TestMethod]
        public void CanSearchBegining()
        {
            string path = @"..\..\TestFiles\tiny.txt";
            FileInfo file = new FileInfo(path);
            WordNetFile fileSource = new WordNetFile(file);
            string expectedValue = "abashedly jkjlhj 777 999 666";
            Assert.AreEqual(expectedValue, fileSource.Search("abashedly").Trim());
            Assert.IsNull(fileSource.Search("abashedl"));

            expectedValue = "catalyze uiouy 676876";
            Assert.AreEqual(expectedValue, fileSource.Search("catalyze").Trim());
            Assert.IsNull(fileSource.Search("catalyz"));

            expectedValue = "genetic zzzz";
            Assert.AreEqual(expectedValue, fileSource.Search("genetic").Trim());
            Assert.IsNull(fileSource.Search("geneti"));
        }
       
        [TestMethod]
        public void CanSearchEnd()
        {
            string path = @"..\..\TestFiles\tiny.txt";
            FileInfo file = new FileInfo(path);
            WordNetFile fileSource = new WordNetFile(file);
            string expectedValue = "satisfactorily wsdrty 678";
            Assert.AreEqual(expectedValue, fileSource.Search("satisfactorily").Trim());
            Assert.IsNull(fileSource.Search("satisfactoril"));

            expectedValue = "kitty-corner dddd 234";
            Assert.AreEqual(expectedValue, fileSource.Search("kitty-corner").Trim());
            Assert.IsNull(fileSource.Search("kitty-cor"));

            expectedValue = "ill-mannered 2234 sssd";
            Assert.AreEqual(expectedValue, fileSource.Search("ill-mannered").Trim());
            Assert.IsNull(fileSource.Search("ill-"));
        }

        [TestMethod]
        public void CanSearchWordNetFile()
        {
            string path = @"..\..\TestFiles\wordnetsample.txt";
            FileInfo file = new FileInfo(path);
            WordNetFile fileSource = new WordNetFile(file);
            string expectedValue = "abo_antibodies n 1 1 @ 1 1 14830318";
            Assert.AreEqual(expectedValue, fileSource.Search("abo_antibodies").Trim());
            Assert.IsNull(fileSource.Search("abo_"));

            expectedValue = "acromphalus n 1 1 @ 1 0 13835934";
            Assert.AreEqual(expectedValue, fileSource.Search("acromphalus").Trim());
            Assert.IsNull(fileSource.Search("acromphal"));

            expectedValue = "air_wave n 1 1 @ 1 1 07246688";
            Assert.AreEqual(expectedValue, fileSource.Search("air_wave").Trim());
            Assert.IsNull(fileSource.Search("air_w"));

            expectedValue = "cyclooxygenase-1 n 1 1 @ 1 0 14543932";
            Assert.AreEqual(expectedValue, fileSource.Search("cyclooxygenase-1").Trim());
            Assert.IsNull(fileSource.Search("cyclooxygenase-"));

            expectedValue = "daisyleaf_grape_fern n 1 1 @ 1 0 12792492";
            Assert.AreEqual(expectedValue, fileSource.Search("daisyleaf_grape_fern").Trim());
            Assert.IsNull(fileSource.Search("daisyleaf_grape_fe"));

            expectedValue = "washing_soda n 1 1 @ 1 0 14846984";
            Assert.AreEqual(expectedValue, fileSource.Search("washing_soda").Trim());
            Assert.IsNull(fileSource.Search("washing_s"));
        }
    }
}
